package org.streets.eis.ext.analysis.pages;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.streets.eis.ext.analysis.QuerySet;

/**
 * 
 * 封装查询的结果集 数据为模板所用
 * 
 * @author chq
 * 
 */
public class VelocityModel implements  Serializable {

	private static final long serialVersionUID = 1L;
	private List<Integer> rows = new ArrayList<Integer>();
	private List<Integer> cols = new ArrayList<Integer>();
	private List<String> colNames = new ArrayList<String>();
	
	private List<Map<Integer,Object>> result = new ArrayList<Map<Integer,Object>>();//结果
	

	//使用QuerySet封装
	public void fill(QuerySet qs){
		cols.clear();
		rows.clear();
		colNames.clear();
		result.clear();

		colNames.addAll(qs.getColumnTitles());
		for(int i = 0 ; i < qs.colSize(); i++){
			  cols.add(i);
		}
		for(int i = 0 ; i < qs.rowSize(); i++){
			  rows.add(i);
		}
        
		for(int row = 0; row < qs.rowSize();row++){ 
        	Map<Integer,Object> map = new HashMap<Integer,Object>(qs.colSize());
            for(int col = 0; col < qs.colSize(); col++){
               map.put(col,qs.getValues(row, col));
            }
            result.add(map);
		  }
	}

	//使用QuerySet封装
	public void fill(QuerySet qs,List<String> titles){
		cols.clear();
		rows.clear();
		colNames.clear();
		result.clear();

		colNames.addAll(qs.getColumnTitles());
		for(int i = 0 ; i < qs.colSize(); i++){
			  cols.add(i);
		}
		for(int i = 0 ; i < qs.rowSize(); i++){
			  rows.add(i);
		}
        
		for(int row = 0; row < qs.rowSize();row++){ 
        	Map<Integer,Object> map = new HashMap<Integer,Object>(qs.colSize());
            for(int col = 0; col < qs.colSize(); col++){
               map.put(col,qs.getValues(row, col));
            }
            result.add(map);
		  }
	}
	
	//使用ResultSet封装
	public void fill(ResultSet rs,List<String> titles){
		cols.clear();
		rows.clear();
		colNames.clear();
		result.clear();
	   
		ResultSetMetaData rsmd;
		try {
			for(int i = 0; i < titles.size(); i++ ){
			  colNames.add(titles.get(i));
			}
			
		  rsmd = rs.getMetaData();
		  int numberOfColumns = rsmd.getColumnCount();
		  
		  for(int i = 0 ; i < numberOfColumns; i++){
			  cols.add(i);
		  }
        
		  int row = 0;
		  while(rs.next()) {
        	rows.add(row);
        	row++;
        	
        	Map<Integer,Object> map = new HashMap<Integer,Object>(numberOfColumns);
            for(int r = 0; r<numberOfColumns; r++){
               map.put(r,rs.getObject(r+1));
            }
            
            result.add(map);
		  }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try{
				if(rs != null) rs.close();
			}catch(Exception e){
				e.printStackTrace();  
			}
		}
		
	}
	
	public Object get(Integer row,Integer col){
		Map<Integer,Object> map =  result.get(row);//行
		return map.get(col);//列
	}

	public void setRows(List<Integer> rows) {
		this.rows = rows;
	}

	public List<Integer> getRows() {
		return rows;
	}

	public void setCols(List<Integer> cols) {
		this.cols = cols;
	}

	public List<Integer> getCols() {
		return cols;
	}

	
	/*public void Testfill(){
		cols.clear();
		rows.clear();
		colNames.clear();
		result.clear();
		
		
		for(int i=0;i<10;i++){
			cols.add(i);
			rows.add(i);
			colNames.add("字段名称"+i);
			Map<Integer,Object> map = new HashMap<Integer,Object>(10);//行值
			result.add(map);
		}
		
		for(int i=0;i<10;i++){
			Map<Integer,Object> map = result.get(i);
			for (int k=0;k<10;k++){
				map.put(k, "第"+i+"行"+k+"列值");
			}
		}
	}

*/
	public void setColNames(List<String> colNames) {
		this.colNames = colNames;
	}

	public List<String> getColNames() {
		return colNames;
	}
}
